package hz.mobileproject.pda.firesafety.presenter;


import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;

import cn.droidlover.xdroidmvp.mvp.XPresent;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.NetError;
import cn.droidlover.xdroidmvp.net.XApi;
import hz.mobileproject.pda.entity.request.Request_data;
import hz.mobileproject.pda.entity.response.Response_data;
import hz.mobileproject.pda.firesafety.activity.FireSafetyActivity;
import hz.mobileproject.pda.listener.CustomNetCallBack;
import hz.mobileproject.pda.network.Api;
import hz.mobileproject.pda.network.Constance;
import hz.mobileproject.pda.utils.Base64;
import hz.mobileproject.pda.utils.HttpRqUtils;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by zmybi on 2017/8/28.
 */

public class PStartFireQingDian extends XPresent<FireSafetyActivity> {

    private final static String TAG = PStartFireQingDian.class.getSimpleName();

    public final static int S_INT_TYPE_LOAD_POLICE_INFO = 1;
    public final static int S_INT_TYPE_FIRE_SAFETY_START = 2;
    public final static int S_INT_TYPE_FIRE_SAFETY_LOAD_LIST = 3;
    public final static int S_INT_TYPE_FIRE_SAFETY_CALLING = 4;
    public final static int S_INT_TYPE_FIRE_SAFETY_END = 5;
    private  Gson gson;
    private String token;


    public PStartFireQingDian() {
        gson = new Gson();

        this.token = getV().getApplicationContext().getSharedPreferences("app_login", Context.MODE_PRIVATE).getString("token","");
    }

    public void setData(String request_data, int type) {
        Request_data data = new Request_data();
        data.setData(Base64.getBase64(request_data));
        String bodyData = gson.toJson(data);   //将请求类转化为请求String
        Log.i(TAG, "FIRE_SAFETY type = " + type + ",bodyData = " + request_data);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),bodyData);
        getDataByBody(body,type);

    }

    private void getDataByBody(RequestBody body, int type) {
        switch (type) {
            case S_INT_TYPE_LOAD_POLICE_INFO:{   //民警基本信息获取
                Api.getGankService().getloadPoliceInfo(body,token)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                Log.e(TAG, "FIRE_SAFETY LOAD_POLICE_INFO ERROR= " + error.getMessage());
                                getV().onNetWorkError(error.getMessage(), S_INT_TYPE_LOAD_POLICE_INFO);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                if("0".equals(response_data.getRtnCode())) {
                                    Log.i(TAG, "FIRE_SAFETY LOAD_POLICE_INFO SUCCESS= " + Base64.getFromBase64(response_data.getData()));
                                    getV().onNetWorkSuccess(Base64.getFromBase64(response_data.getData()), S_INT_TYPE_LOAD_POLICE_INFO);
                                }else {
                                    Log.e(TAG, "FIRE_SAFETY LOAD_POLICE_INFO ERROR= " + response_data.getRtnMsg());
                                    getV().onNetWorkError(response_data.getRtnCode(), S_INT_TYPE_LOAD_POLICE_INFO);
                                }
                            }
                        });

            }
            break;
            case S_INT_TYPE_FIRE_SAFETY_START: {   //开始
                Api.getGankService().startFireFighting(body)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                Log.e(TAG, "FIRE_SAFETY_START ERROR= " + error.getMessage());
                                getV().onNetWorkError(error.getMessage(), S_INT_TYPE_FIRE_SAFETY_START);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                if("0".equals(response_data.getRtnCode())) {
                                    Log.i(TAG, "FIRE_SAFETY_START SUCCESS= " + Base64.getFromBase64(response_data.getData()));
                                    getV().onNetWorkSuccess(Base64.getFromBase64(response_data.getData()), S_INT_TYPE_FIRE_SAFETY_START);
                                }else {
                                    Log.e(TAG, "FIRE_SAFETY_START ERROR= " + response_data.getRtnMsg());
                                    getV().onNetWorkError(response_data.getRtnCode(), S_INT_TYPE_FIRE_SAFETY_START);
                                }
                            }
                        });
            }
            break;
            case S_INT_TYPE_FIRE_SAFETY_LOAD_LIST: {   //加载列表
                Api.getGankService().queryFireFighting(body)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                Log.e(TAG, "FIRE_SAFETY_LOAD_LIST ERROR= " + error.getMessage());
                                getV().onNetWorkError(error.getMessage(), S_INT_TYPE_FIRE_SAFETY_LOAD_LIST);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                if("0".equals(response_data.getRtnCode())) {
                                    Log.i(TAG, "FIRE_SAFETY_LOAD_LIST SUCCESS= " + Base64.getFromBase64(response_data.getData()));
                                    getV().onNetWorkSuccess(Base64.getFromBase64(response_data.getData()), S_INT_TYPE_FIRE_SAFETY_LOAD_LIST);
                                }else {
                                    Log.e(TAG, "FIRE_SAFETY_LOAD_LIST ERROR= " + response_data.getRtnMsg());
                                    getV().onNetWorkError(response_data.getRtnCode(), S_INT_TYPE_FIRE_SAFETY_LOAD_LIST);
                                }
                            }
                        });
            }
            break;
            case S_INT_TYPE_FIRE_SAFETY_END: {   //结束
                Api.getGankService().end(body)
                        .compose(XApi.<Response_data>getApiTransformer())
                        .compose(XApi.<Response_data>getScheduler())
                        .compose(getV().<Response_data>bindToLifecycle())
                        .subscribe(new ApiSubscriber<Response_data>() {
                            @Override
                            protected void onFail(NetError error) {
                                Log.e(TAG, "FIRE_SAFETY_END ERROR= " + error.getMessage());
                                getV().onNetWorkError(error.getMessage(), S_INT_TYPE_FIRE_SAFETY_END);
                            }

                            @Override
                            public void onNext(Response_data response_data) {
                                if("0".equals(response_data.getRtnCode())) {
                                    Log.i(TAG, "FIRE_SAFETY_END SUCCESS");
                                    getV().onNetWorkSuccess(response_data.getRtnMsg(), S_INT_TYPE_FIRE_SAFETY_END);
                                }else {
                                    Log.e(TAG, "FIRE_SAFETY_END ERROR= " + response_data.getRtnCode());
                                    getV().onNetWorkError(response_data.getRtnCode(), S_INT_TYPE_FIRE_SAFETY_END);
                                }
                            }
                        });
            }
            break;
        }
    }



    public void startCalling(String request_data, ArrayList<File> filePath) {
//        Request_data data = new Request_data();
//        data.setData(Base64.getBase64(request_data));
//        String bodyData = gson.toJson(data);
//        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"), bodyData);
//        calling(bodyData,filePath,body);

        //0302   注释by lc
//        HttpRqUtils.httpPost(Constance.BASE_URL + Constance.URL_FIRE_CALLING, request_data, null, new CustomNetCallBack() {
//            @Override
//            public void onSuccessFromSer(int rtnCode, String rtnMsg, String data) {
//                Log.i(TAG, "FIRE_SAFETY_CALLING SUCCESS=" + data);
//                getV().onNetWorkSuccess(data, S_INT_TYPE_FIRE_SAFETY_CALLING);
//            }
//
//            @Override
//            public void onErrorFromNet(Throwable ex, boolean isOnCallback) {
//                Log.e(TAG, "FIRE_SAFETY_CALLING ERROR= " + ex.getMessage());
//                getV().onNetWorkError(ex.getMessage(), S_INT_TYPE_FIRE_SAFETY_CALLING);
//            }
//        });
    }


//    private void calling(String body_data, ArrayList<File> filePath, RequestBody body) {
//        if (filePath != null && filePath.size() > 0) {
//            BaiduParams_firePic params = new BaiduParams_firePic();
//            for (int i = 0; i < filePath.size(); i++) {
//                File file = filePath.get(i);
//                params.addBodyParameter("file", file, "application/octet-stream");
//            }
//            params.addBodyParameter("data", body_data);
//            x.http().post(params, new Callback.CommonCallback<List<BaiduResponse>>() {
//
//
//                @Override
//                public void onSuccess(List<BaiduResponse> result) {
//                    String jsonString = result.get(0).getTest();
//                    try {
//                        JSONObject jsonObject = new JSONObject(jsonString);
//                        String code = jsonObject.getString("rtnCode");
//                        if (code.equals("0")){
//                            String data = Base64.getFromBase64(jsonObject.getString("data"));
//                            Log.i(TAG, "FIRE_SAFETY_CALLING SUCCESS=" + data);
//                            getV().onNetWorkSuccess(data, S_INT_TYPE_FIRE_SAFETY_CALLING);
//                        }else {
//                            Log.e(TAG, "FIRE_SAFETY_CALLING ERROR= " + code);
//                            getV().onNetWorkError(code, S_INT_TYPE_FIRE_SAFETY_CALLING);
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//
//                @Override
//                public void onError(Throwable ex, boolean isOnCallback) {
//                    Log.e(TAG, "FIRE_SAFETY_CALLING ERROR= " + ex.getMessage());
//                    getV().onNetWorkError(ex.getMessage(), S_INT_TYPE_FIRE_SAFETY_CALLING);
//                }
//
//                @Override
//                public void onCancelled(CancelledException cex) {
//                }
//
//                @Override
//                public void onFinished() {
//                }
//            });
//
//        } else {
//            //图片路径集合为空
//            RequestParams params = new RequestParams(Constance.BASE_URL + "/PrisonAppB/fireFighting/calling1");
//            params.setBodyContent(body_data);
//            x.http().post(params, new Callback.CommonCallback<String>() {
//                @Override
//                public void onSuccess(String result) {
//                    try {
//                        JSONObject jsonObject = new JSONObject(result);
//                        String code = jsonObject.getString("rtnCode");
//                        if (code.equals("0")){
//                            String data = Base64.getFromBase64(jsonObject.getString("data"));
//                            Log.i(TAG, "FIRE_SAFETY_CALLING SUCCESS=" + result);
//                            getV().onNetWorkSuccess(data, S_INT_TYPE_FIRE_SAFETY_CALLING);
//                        }else {
//                            Log.e(TAG, "FIRE_SAFETY_CALLING ERROR= " + code);
//                            getV().onNetWorkError(code, S_INT_TYPE_FIRE_SAFETY_CALLING);
//                        }
//                    } catch (JSONException e) {
//                        e.printStackTrace();
//                    }
//                }
//
//                @Override
//                public void onError(Throwable ex, boolean isOnCallback) {
//                    Log.e(TAG, "FIRE_SAFETY_CALLING ERROR= " + ex.getMessage());
//                }
//
//                @Override
//                public void onCancelled(CancelledException cex) {
//
//                }
//
//                @Override
//                public void onFinished() {
//
//                }
//            });
//        }
//    }
}